<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" :rules="rules" label-width="78px">
      <!--搜索条件-->
      <el-row>
        <el-col :span="6">
          <el-form-item label="生成日期" prop="generateDate">
            <el-date-picker clearable
                            v-model="queryParams.generateDate"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择待办事项生成日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="9">
          <el-form-item label="优化日期" prop="optimizeDate">
            <el-date-picker
              v-model="queryParams.optimizeDate"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @keyup.enter.native="handleQuery">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="统计周期" prop="statCycle">
            <el-select v-model="queryParams.statCycle" placeholder="请选择">
              <el-option label="7天" :value="7"/>
              <el-option label="15天" :value="15"/>
              <el-option label="30天" :value="30"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="7">
          <el-form-item label="店铺" prop="shopId">
            <el-select v-model="queryParams.storeId" filterable clearable @keyup.enter.native="handleQuery" placeholder="请选择店铺">
              <el-option v-for="item in storeMgmtList" :key="item.id" :label="item.storeAbbr" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label="负责人" prop="leaderName">
            <el-input v-model="queryParams.leaderName" placeholder="请输入负责人"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="优化时长" prop="optimizationDuration">
            <el-input v-model="queryParams.optimizationDurationMin" placeholder="请输入"
                      style="width: 150px;"></el-input>
            <span> - </span>
            <el-input v-model="queryParams.optimizationDurationMax" placeholder="请输入"
                      style="width: 150px;"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="支付金额" prop="payAmount">
            <el-input v-model="queryParams.payAmountMin" placeholder="请输入" style="width: 150px;"></el-input>
            <span> - </span>
            <el-input v-model="queryParams.payAmountMax" placeholder="请输入" style="width: 150px;"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="商品访客数" prop="goodsVisitorNumber" label-width="90px">
            <el-input v-model="queryParams.goodsVisitorNumberMin" placeholder="请输入"
                      style="width: 150px;"></el-input>
            <span> - </span>
            <el-input v-model="queryParams.goodsVisitorNumberMax" placeholder="请输入"
                      style="width: 150px;"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="收藏加购率" prop="optimizationDuration" label-width="90px">
            <el-input v-model="queryParams.collectTreasureBuyRateMin" placeholder="请输入"
                      style="width: 150px;"></el-input>
            <span> - </span>
            <el-input v-model="queryParams.collectTreasureBuyRateMax" placeholder="请输入"
                      style="width: 150px;"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        <el-button icon="el-icon-download" @click="handleExport">下载</el-button>
      </el-form-item>
    </el-form>

    <el-row>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="resetQuery"></right-toolbar>
    </el-row>

    <div ref="divTable">
      <el-table :height="showSearch?'46vh':'68vh'" :key="dataTotal" :summary-method="getScoreSummaries"  :show-summary="showSummary" v-loading="loading" :data="replayDataList" @sort-change='sortChange'>
        <el-table-column label="商品ID" align="center" prop="goodsId"/>
        <el-table-column label="商品图片" align="center" prop="imgUrl">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.imgUrl"
              :preview-src-list="[scope.row.imgUrl]"
            >
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline"></i>
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="优化日期"  align="center" prop="optimizeDate" sortable='custom'/>
        <el-table-column label="复盘周期"  align="center" prop="statCycle"/>
        <el-table-column label="支付金额"  align="center" prop="payAmount" sortable='custom'/>
        <el-table-column label="商品访客数" align="center" prop="goodsVisitorNumber" sortable='custom'/>
        <el-table-column label="收藏加购率" align="center" prop="collectTreasureBuyRate" sortable='custom'/>
        <el-table-column label="负责人" align="center" prop="leaderName"/>
        <el-table-column label="优化时长" align="center" prop="optimizationDuration" sortable='custom'/>
        <el-table-column label="评价结果" align="center" prop="optimizeResult"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :pageRefs="this.$refs"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 修改评价结果对话框 -->
    <el-dialog title="修改评价结果" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="评价结果" prop="optimizeResult">
          <el-input v-model="form.optimizeResult" placeholder="请输入评价结果"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {parseTime} from "@/utils/basic";
import {exportUnsalableReplayData, getReplayCollectData, unsalableReplayData} from "@/api/business/unsalableBoard";
import {getUnsalableBacklog, updateUnsalableBacklog} from "@/api/business/unsalableBacklog";
import {getNormalStoreMgmtList} from "@/api/business/storeMgmt";

export default {
  name: "UnsalableReplay",
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      open: false,
      // 表单参数
      form: {},
      //链接复盘数据
      replayDataList: [],
      queryParams: {
        generateDate: null,
        optimizeDate: [],
        optimizeDateStart:null,
        optimizeDateEnd:null,
        statCycle: 7,
        leaderName: "",
        payAmountMin:null,
        payAmountMax:null,
        optimizationDurationMin: null,
        optimizationDurationMax: null,
        goodsVisitorNumberMin: null,
        goodsVisitorNumberMax: null,
        collectTreasureBuyRateMin: null,
        collectTreasureBuyRateMax: null
      },
      rules: {
        optimizeDate: [
          {required: true, message: '请选择优化日期', trigger: 'blur'},
        ],
        statCycle: [
          {required: true, message: '请选择统计周期', trigger: 'blur'},
        ]
      },
      //合计数据
      collectData:{},
      //展示合计
      showSummary:false,
      //控制刷新表格
      dataTotal:0,
      storeMgmtList: [],
    }
  },
  created() {
    this.getNormalStoreMgmtList();
  },
  methods: {
    /** 查询店铺列表 */
    getNormalStoreMgmtList() {
      getNormalStoreMgmtList().then(res => {
        this.storeMgmtList = res.data
      })
    },
    //后端全页面动态排序
    sortChange(column){
      this.queryParams.order=null
      this.queryParams.prop=null
      if (column.order==="ascending"){
        this.queryParams.order="asc"
      }else if (column.order==="descending"){
        this.queryParams.order="desc"
      }

      this.queryParams.prop=column.prop;

      this.getList()
    },
    getList() {
      if (this.queryParams.optimizeDate===null||this.queryParams.optimizeDate===[]||this.queryParams.optimizeDate.length===0){
        this.$message.error("请选择优化日期")
        return false
      }
      this.loading = true;
      if (this.queryParams.optimizeDate.length > 0){
        this.queryParams.optimizeDateStart=parseTime(this.queryParams.optimizeDate[0])
        this.queryParams.optimizeDateEnd=parseTime(this.queryParams.optimizeDate[1])
      }
      unsalableReplayData(this.queryParams).then(response => {
        this.replayDataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.getCollectData();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$resetForm("queryForm");
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        generateDate: null,
        optimizeDate: [],
        optimizeDateStart:null,
        optimizeDateEnd:null,
        statCycle: 7,
        leaderName: "",
        optimizationDurationMin: null,
        optimizationDurationMax: null
      }
      this.replayDataList = []
      this.total = 0;
    },
    handleUpdate(row) {
      this.reset();
      const id = row.logId
      getUnsalableBacklog(id).then(response => {
        this.form.id = id
        this.form.optimizeResult = response.data.optimizeResult
        this.open = true;
      })
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form, "表单数据")
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateUnsalableBacklog(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        optimizeResult: null,
      };
      this.$resetForm("form");
    },
    //获取合计数据
    getCollectData(){
      if (this.queryParams.optimizeDate===null||this.queryParams.optimizeDate===[]||this.queryParams.optimizeDate.length===0){
        return false
      }

      if (this.queryParams.optimizeDate.length > 0){
        this.queryParams.optimizeDateStart=parseTime(this.queryParams.optimizeDate[0])
        this.queryParams.optimizeDateEnd=parseTime(this.queryParams.optimizeDate[1])
      }

      getReplayCollectData(this.queryParams).then(response=>{
        this.collectData=response.data
        //展示合计
        this.showSummary=true
        this.dataTotal = new Date().getTime()
      })
    },
    //返回表格合计数据
    getScoreSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
        }else if (column.property==='imgUrl'||column.property==='optimizeDate'||column.property==='leaderName'||column.property==='optimizeResult'){
          sums[index] = '-';
        }else {
          sums[index]=this.collectData[column.property]
        }
      })
      return sums
    },
    //下载导出数据
    handleExport(){
      if (this.replayDataList.length===0){
        this.$modal.msgWarning("请先查询数据后下载");
        return
      }
      if (this.queryParams.optimizeDate===null||this.queryParams.optimizeDate===[]||this.queryParams.optimizeDate.length===0){
        this.$message.error("请选择优化日期")
        return
      }
      if (this.queryParams.optimizeDate.length > 0){
        this.queryParams.optimizeDateStart=parseTime(this.queryParams.optimizeDate[0])
        this.queryParams.optimizeDateEnd=parseTime(this.queryParams.optimizeDate[1])
      }
      exportUnsalableReplayData(this.queryParams)
    }
  }
}
</script>

<style scoped lang="scss">

</style>
